import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseInterceptors,
  UploadedFile,
  Query
} from '@nestjs/common'
import * as xlsx from 'xlsx'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import {
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiTags
} from '@nestjs/swagger'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { LogService } from './log.service'
import { CreateLogDto } from './dto/create-log.dto'
import { UpdateLogDto } from './dto/update-log.dto'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('日志')
@Controller('log')
export class LogController {
  constructor(private readonly LogService: LogService) {}

  @Post()
  @ApiOperation({ summary: '创建日志' })
  @ApiBody({ type: CreateLogDto, description: '日志信息' })
  async create(@Body() createLogDto: CreateLogDto) {
    return FResponse.success(await this.LogService.create(createLogDto))
  }
  @Post('upload')
  @ApiOperation({ summary: '批量创建日志' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateLogDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.LogService.create(item)))
    return FResponse.success('批量创建成功')
  }
  @Post('all')
  @ApiOperation({ summary: '获取所有日志' })
  @ApiBody({
    description: '日志信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.LogService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete
      })
    )
  }

  // 获取模块分布
  @Get('module')
  @ApiOperation({ summary: '获取模块分布' })
  async getModule() {
    return FResponse.success(await this.LogService.getModule())
  }

  // 获取操作信息按时间分组
  @Get('time')
  @ApiOperation({ summary: '获取操作信息按时间分组' })
  async getTime() {
    return FResponse.success(await this.LogService.getTime())
  }

  // 获取操作信息按天分组近七天
  @Get('day')
  @ApiOperation({ summary: '获取操作信息按天分组近七天' })
  async getDay() {
    return FResponse.success(await this.LogService.getDay())
  }

  // 按操作类型 统计用户的操作行为
  @Get('method')
  @ApiOperation({ summary: '按操作类型 统计用户的操作行为' })
  async getMethod() {
    return FResponse.success(await this.LogService.getMethod())
  }

  // 按操作结果 统计用户的操作行为
  @Get('code')
  @ApiOperation({ summary: '按操作结果 统计用户的操作行为' })
  async getCode() {
    return FResponse.success(await this.LogService.getCode())
  }

  // 按用户 统计用户的操作行为 用户名 用户角色 用户部门 用户职位
  @Get('user')
  @ApiOperation({
    summary: '按用户 统计用户的操作行为 用户名 用户角色 用户部门 用户职位'
  })
  @ApiQuery({ name: 'staffId', description: '用户ID', required: false })
  @ApiQuery({ name: 'roleId', description: '角色ID', required: false })
  @ApiQuery({ name: 'departmentId', description: '部门ID', required: false })
  @ApiQuery({ name: 'jobId', description: '职位ID', required: false })
  async getUser(
    @Query('staffId') staffId?: number,
    @Query('roleId') roleId?: number,
    @Query('departmentId') departmentId?: number,
    @Query('jobId') jobId?: number
  ) {
    if (!staffId && !roleId && !departmentId && !jobId)
      return FResponse.success([])
    return FResponse.success(
      await this.LogService.getUser(staffId, roleId, departmentId, jobId)
    )
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改指定日志' })
  @ApiParam({ name: 'id', description: '日志ID' })
  @ApiBody({ type: UpdateLogDto, description: '日志信息' })
  async update(@Param('id') id: string, @Body() updateLogDto: UpdateLogDto) {
    return FResponse.success(await this.LogService.update(+id, updateLogDto))
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除日志' })
  @ApiBody({ type: [Number], description: '日志ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.LogService.remove(ids))
  }

  // 彻底删除
  @Delete('thorough')
  @ApiOperation({ summary: '批量彻底删除日志' })
  @ApiBody({ type: [Number], description: '日志ID数组' })
  async batchThoroughRemove(@Body() ids: number[]) {
    return FResponse.success(await this.LogService.thoroughRemove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复日志' })
  @ApiBody({ type: [Number], description: '日志ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.LogService.restore(ids))
  }
}
